package com.topscomm.base;

import com.topscomm.basic.exception.BusinessException;
import com.topscomm.mis.MisController;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.OfficeOperateUtil;
import com.topscomm.tap.TapController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class BaseController<T extends BaseEntity, TService extends IBaseService<T, ?>>
		extends MisController<T, TService> implements IBaseController<T, TService> {


	/**
	 * @description: 导出统计数据
	 * @author: zhangqianbo
	 * @date:
	 * @modify:
	 */
	public void exportBaseExcel(Map<String, Object> paraMap, HttpServletRequest request,
							   HttpServletResponse response, List<Map<String, Object>> listDataMap, String fileName) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			String cols = ConvertUtil.convertToString(paraMap.get("cols"));
			String[] colArray = cols.split(",");
			InputStream inStream = OfficeOperateUtil.exportExeclXlsx(listDataMap, colArray);
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.setHeader("FileName", URLEncoder.encode(fileName, "utf-8"));
			response.setHeader("Access-Control-Expose-Headers", "FileName");
			OutputStream outStream = response.getOutputStream();
			byte[] bytes = new byte[2048];
			int len;
			while ((len = inStream.read(bytes)) != -1) {
				outStream.write(bytes, 0, len);
			}
			inStream.close();
		} catch (Exception ex) {
			throw new BusinessException(ex.getMessage(), true);
		}
	}
}
